//JavaScript Document
$(function () {
		$(".cur_postion").html("当前位置： [ "+sessionStorage.getItem("pmenuname")+" ] - [ "+sessionStorage.getItem("cmenuname")+" ]");
		
		//查询条件
		var strhtml_searchContent = '<div class="inline-block margin">'
			+'<span>订单编号:</span>'
			+'<input type="text" class="inputStyle_condition crporGoodsName"/>'
			+'</div>';
		$(".searchContent").html(strhtml_searchContent);
		//是否显示查询条件
		showSearchBox(true);
		
		var obj = {};//查询条件对象
		searchContent(obj);
		showList(obj,1);
		
		//详情
		$("body").delegate('.detailit','click', function(){  //alert($(this).attr("data-id"));
			layer.open({
		        type: 2,
		        title: '详情',
		        scrollbar: false,
		        maxmin: true,
		        shadeClose: false, //点击遮罩关闭层
		        area : ['800px' , '520px'],
		        content: '../mall/order_detailCart.html?id='+$(this).attr("data-id")
		    });
		});
		//修改
		$("body").delegate('.modifyit','click', function(){
			layer.open({
		        type: 2,
		        title: '修改',
		        scrollbar: false,
		        maxmin: true,
		        shadeClose: false, //点击遮罩关闭层
		        area : ['800px' , '520px'],
		        content: '../mall/order_modifyCart.html?id='+$(this).attr("data-id")
		    });
		});
		
		//全选
		$("body").delegate("input[name='checkboxAll']","click",function(){
			if($(this).attr("checked")){
				$("input[name='checkbox']").each(function(){
					$(this).attr("checked",true);
				});
			}else{
				$("input[name='checkbox']").each(function(){
					$(this).removeAttr("checked");
				});
			}			
		});
		
		//tr高亮显示
		$("body").delegate(".trHighLight","mouseover",function(){
			$(this).find("td").css("background-color","#c1ebff");
		});
		$("body").delegate(".trHighLight","mouseleave",function(){
			$(this).find("td").css("background-color","#fff");
		});
		//查询
		$("body").delegate(".searchBtn","click",function(){
			var obj = {};//查询条件对象
			searchContent(obj);
			showList(obj,1);
		});
		//重置
		$("body").delegate(".resetBtn","click",function(){
			var obj = {};//查询条件对象
			$(".crrrdTitle").val('');
			searchContent(obj);
			showList(obj,1);
		});
		//上一页
		$('.manageBox').delegate(".backpage","click", function() {
			var obj = {};//查询条件对象
			searchContent(obj);
			pagenum = parseInt($(this).attr("data-pagenum"));
			showList(obj,pagenum);
		}); 
		//下一页
		$('.manageBox').delegate(".nextpage","click", function() {
			var obj = {};//查询条件对象
			searchContent(obj);
			var pagenum = parseInt($(this).attr("data-pagenum"));
			showList(obj,pagenum);
		}); 
		//首页
		$('.manageBox').delegate(".firstpage","click", function() {
			var obj = {};//查询条件对象
			searchContent(obj);
			var pagenum = parseInt($(this).attr("data-pagenum"));
			showList(obj,pagenum);
		}); 
		//末页
		$('.manageBox').delegate(".lastpage","click", function() {
			var obj = {};//查询条件对象
			searchContent(obj);
			var pagenum = parseInt($(this).attr("data-pagenum"));
			showList(obj,pagenum);
		}); 
		//跳转至
		$('.manageBox').delegate(".jumppage","click", function() {
			var obj = {};//查询条件对象
			searchContent(obj);
			var pagenum = parseInt($('.jumppagetext').val());
			if(pagenum>0 && pagenum <=parseInt($(this).attr("data-pagemax"))){
				showList(obj,pagenum);
			}else{
				alert("查无此页！");
			}
		});		
	});
	
	function showList(obj,pagenum){
		var aData = [{name:"<input type='checkbox' name='checkboxAll' value='checkbox' />",percent:"7"},
		             {name:"订单编号",percent:"10"},
		             {name:"下单用户",percent:"10"},
		             {name:"创建日期",percent:"10"},
		             {name:"状态",percent:"10"},
		             {name:"总价",percent:"10"},
		             {name:"支付价格",percent:"10"},
		             {name:"所用积分",percent:"10"},
		             {name:"抵扣费用",percent:"10"},
		             {name:"操作",percent:"15"}];
		setTableHead(aData);
		var str = '';
		if(obj.crporUuid == undefined || obj.crporUuid==""){
			str = 'pageNum='+pagenum+'&pageSize=10';
		}else{
			str = 'pageNum='+pagenum+'&pageSize=10&orderUuid='+encodeURIComponent(obj.crporUuids);
		}
		
		getOData(str,"coreCartOrder/find/by/cnd",{fn:function(oData){
					var strhtml_list = "";
					var arrData = oData.data.result;
					var ln = arrData.length;
					for(var i=0;i<ln;i++){
						var imgclick = "";
						if(arrData[i].list != undefined){
							var cratmUuid = arrData[i].list[0].cratmUuid;
						}
						strhtml_list += '<tr class="trHighLight" data-imageurl="'+imgclick+'">'
							+'<td>'+'<input type="checkbox" name="checkbox" value="checkbox" data-id="'+arrData[i].crporUuid+'"/>'+'</td>'
							+'<td>'+(arrData[i].crporUuid || "")+'</td>'
							+'<td>'+(arrData[i].crporUserName || "")+'</td>'
							+'<td>'+(arrData[i].crporCdate || "")+'</td>';
						var crporStatus="";
						if(arrData[i].crporStatus ==2){
							crporStatus="支付成功";
						} else if (arrData[i].crporStatus ==3) {
							crporStatus="支付失败";
						} else if (arrData[i].crporStatus ==4) {
							crporStatus="已发货";
						} else if (arrData[i].crporStatus ==5) {
							crporStatus="交易成功";
						}
						strhtml_list +='<td>'+(crporStatus || "")+'</td>'
						 	+'<td>'+(arrData[i].crporTotal || "0")+'</td>'
						 	+'<td>'+(arrData[i].crporSjFee || "0")+'</td>'
						 	+'<td>'+(arrData[i].crporSyScore || "0")+'</td>'
						 	+'<td>'+(arrData[i].crporDkFee || "0")+'</td>'
				       		+'<td>'
				       		+'<a class="p-edit detailit" data-id="'+arrData[i].crporUuid+'">查看</a>'
				       		+'<a class="p-edit modifyit" data-id="'+arrData[i].crporUuid+'" >修改</a>'
				            +'</td>'
				            +'</tr>';
					}
					$(".tb-body").html(strhtml_list);
					setTableFoot(oData.data,aData.length);
			}}
		);
	}
	
	function isNull(obj,pagenum){
		var str = '';
		if(obj.crporUuid==""){
			str = 'pageNum='+pagenum+'&pageSize=10';
		}else{
			str = 'pageNum='+pagenum+'&pageSize=10&crporUuid='+encodeURIComponent(obj.crporUuid);
		}
		getOData(str,"coreCartOrder/find/by/cnd",{fn:function(oData){
				var arrData = oData.data.result;
				var ln = arrData.length;
				if(ln == 0){
					if (oData.data.totalCount != 0 && pagenum !=1){
						showList(obj,pagenum-1);
					}else{
						showList(obj,1);
					}
				}else{
					showList(obj,pagenum);
				}
			}}
		);
	}
	
	function refreshList(){
		var obj = {};//查询条件对象
		searchContent(obj);
		showList(obj,parseInt($(".curpage").text()));
		layer.closeAll();
	}
	
	function searchContent(obj){
		obj.crporUuid = $(".crporUuid").val();
	}
